package com.shequan.demo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ailk.dazzle.util.data.JSONUtils;
import com.shequan.common.bean.ResultDataStruct;
import com.shequan.demo.bean.pojo.DemoUserInfo;
import com.shequan.demo.service.DemoService;
/**
 * <P>
 * Copyright 2012 shequaner.
 * All Rights Reserved.
 * @author Liust
 * @version 1.0, 2012-2-20
 * @see com.shequan.demo.controller.DemoController 
 * @since 1.0
 */
@Controller
@RequestMapping("/demo")
public class DemoController{
	
	private static Logger log = Logger.getLogger(DemoController.class);
	
	private DemoService demoService;

	public void setDemoService(DemoService demoService) {
		this.demoService = demoService;
	}

	/**
	 * 列举所有用户信息
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/all")
	public ModelAndView getUserInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ResultDataStruct<List<DemoUserInfo>> rds = new ResultDataStruct<List<DemoUserInfo>>();
		try {
			List<DemoUserInfo> users = demoService.getAllUsers();
			rds.setData(users);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			throw e;
		}
		request.setAttribute("rds", rds);
		return new ModelAndView("/pages/demo/user.jsp");
	}

	/**
	 * 用户登陆
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/login")
	public void login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.info("----in loginUser---");
		ResultDataStruct<String> rds = new ResultDataStruct<String>();
		try {
			String userName = request.getParameter("userName");
			String userPwd = request.getParameter("userPwd");
			log.debug(userPwd);
			DemoUserInfo userInfo = demoService.getUserInfo4Login(userName, userPwd);
			if(userInfo==null){
				rds.setBusiSuccess(false);
				rds.setMessage("登录失败，用户名或密码错误");
			}else{
				rds.setBusiSuccess(true);
				rds.setMessage("登录成功");
				request.getSession().setAttribute("user", userInfo);
			}
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			rds.setSuccess(false);
			rds.setMessage(e.getMessage());
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}
	
	/**
	 * 注册用户
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/regist")
	public void regist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.info("----in registUser---");
		ResultDataStruct<Integer> rds = new ResultDataStruct<Integer>();
		try {
			String userName = request.getParameter("userName");
			String password = request.getParameter("userPwd");
			rds = demoService.registUser(userName, password);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			rds.setSuccess(false);
			rds.setMessage(e.getMessage());
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}
}
